package com.net.oil.main.fragment;

import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.base.BaseFragment;
import com.net.oil.main.adapter.TechnologyViewPagerAdapter;
import com.net.oil.main.bean.TechnologyTypeBean;
import com.net.oil.main.contract.TechnologySupportContract;
import com.net.oil.main.presenter.TechnologySupportPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class TechnologySupportFragment extends BaseFragment<TechnologySupportPresenter> implements TechnologySupportContract.View {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.technology_top_tab)
    TabLayout technologyTopTab;
    @BindView(R.id.technology_view_pager)
    ViewPager technologyViewPager;
    Unbinder unbinder;

    private TechnologyViewPagerAdapter viewPagerAdapter;

    public static TechnologySupportFragment newInstance() {
        return new TechnologySupportFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_technology_support, container, false);
        presenter = new TechnologySupportPresenter(this);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initData();
        return view;
    }

    private void initData() {
        presenter.getTechnologyType();
    }

    private void initView() {
        toolbarTitle.setText("技术支援");
        toolbarGoBack.setVisibility(View.GONE);

        viewPagerAdapter = new TechnologyViewPagerAdapter(getActivity().getSupportFragmentManager());
        technologyTopTab.setupWithViewPager(technologyViewPager);
        technologyViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(technologyTopTab));
        technologyViewPager.setAdapter(viewPagerAdapter);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 获取技术支持成功
     * @param technologyTypeBeans   数据
     */
    @Override
    public void getTechnologyTypeSuccess(List<TechnologyTypeBean> technologyTypeBeans) {
        if (technologyTypeBeans != null){
            viewPagerAdapter.updateData(technologyTypeBeans);
        }
    }

    /**
     * 获取技术支持失败
     */
    @Override
    public void getTechnologyTypeFailed() {

    }
}
